package com.benzhuo.widget.research.recycler;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

/**
 * 实现了一个GIF图片展示的加载更多footview。
 * Created by WuWangchun on 2016/7/21.
 */
public class ZGifLoadMoreView extends RelativeLayout implements ZLoadRecyclerView.ILoadStateSet{
    private ImageView gifView;//GIF图片
    private TextView textView;//文本
    private String loading;
    private String fail;
    private String noMore;
    public ZGifLoadMoreView(Context context) {
        super(context);
        init(context);
    }

    public ZGifLoadMoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ZGifLoadMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ZGifLoadMoreView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context){
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        float height = (metrics.densityDpi/160f)*50;
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)height));
        LinearLayout layout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        float px = 5 * (metrics.densityDpi / 160f);
        params.leftMargin=(int)px;
        gifView = new ImageView(context);
        gifView.setVisibility(GONE);
        layout.addView(gifView,params);
        textView = new TextView(context);
        textView.setTextColor(0xff333333);
        textView.setTextSize(13);
        textView.setVisibility(GONE);
        layout.addView(textView,params);

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        this.addView(layout,layoutParams);
    }

    public void setBitmapResource(int resourceId){
        Drawable drawable = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawable = getContext().getResources().getDrawable(resourceId, getContext().getTheme());
        } else {
            drawable = getContext().getResources().getDrawable(resourceId);
        }
        Glide.with(this.getContext()).load(resourceId).override(drawable.getMinimumWidth(),drawable.getMinimumHeight()).into(gifView);
        gifView.setVisibility(VISIBLE);
    }

    public void setText(String loading,String fail,String noMore){
        this.loading=loading;
        this.fail=fail;
        this.noMore=noMore;
        textView.setVisibility(VISIBLE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void beginLoading() {
        textView.setText(loading);
    }

    @Override
    public int stopLoading(State state) {
        int retain = 0;
        switch (state){
            case LOAD_SUCCESS:
                break;
            case LOAD_FAIL:
                textView.setText(fail);
                retain=2000;
                break;
            case LOAD_NO_MORE:
                textView.setText(noMore);
                retain=2000;
                break;
        }
        return retain;
    }
}
